package com.incar.chuxing.utils.Dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.incar.chuxing.R;
import com.incar.chuxing.utils.Logger;
import com.incar.chuxing.utils.SPKey;
import com.incar.chuxing.utils.SPUtils;

public class ConfirmAbatementDialog extends Dialog {
    Context mContext;
    boolean isCheckedDefault;
    String abatementPrice;
    private TextView checkNo, checkOk, checkDefault, checkOnce;
    private OnCheckResultListener checkResultListener;//确定按钮被点击了的监听器


    public ConfirmAbatementDialog(Context context) {
        super(context, R.style.navi_dialog);
        this.mContext = context;

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (TextUtils.isEmpty(SPUtils.getInstance(mContext).getStringValue(SPKey.ABATEMENT_CHECK)) ||
                SPUtils.getInstance(mContext).getStringValue(SPKey.ABATEMENT_CHECK).equals("0")) {
            isCheckedDefault = false;
        } else {
            isCheckedDefault = true;
        }
        Logger.i("checkedee", isCheckedDefault + "");

        setContentView(isCheckedDefault ? R.layout.layout_abatement_uncheck : R.layout.layout_abatement_checked);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        window.setWindowAnimations(R.style.dialog_choose_navi);
        initView();
        initEvent();
    }


    /**
     * 初始化界面的确定和取消监听器
     */
    public void setData(String abatementPrice) {
        this.abatementPrice = abatementPrice;
    }


    /**
     * 初始化界面控件
     */
    private void initView() {
        checkNo = findViewById(R.id.check_no);
        checkDefault = findViewById(R.id.check_default);
        checkOnce = findViewById(R.id.check_once);
        checkOk = findViewById(R.id.check_ok);
        String rawAbatementTip = SPUtils.getInstance(mContext).getStringValue(SPKey.ABATEMENT_TIP);
        if (rawAbatementTip != null) {
            ((TextView) findViewById(R.id.desc)).setText(rawAbatementTip.replace("#bujimianpei#", abatementPrice));
        }
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        checkDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkResultListener != null) {
                    checkResultListener.onCheckDefault();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        checkNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkResultListener != null) {
                    checkResultListener.onCheckNo();
                }
            }
        });

        checkOnce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkResultListener != null) {
                    checkResultListener.onCheckOnce();
                }
            }
        });

        checkOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkResultListener != null) {
                    checkResultListener.onCheckOk();
                }
            }
        });
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface OnCheckResultListener {
        void onCheckDefault();

        void onCheckNo();

        void onCheckOnce();

        void onCheckOk();
    }


    public void setOnCheckResultListener(OnCheckResultListener checkResultListener) {
        this.checkResultListener = checkResultListener;
    }

}